package cn.tedu._05mvcboot04.base.exception;

import cn.tedu._05mvcboot04.base.response.JsonResult;
import cn.tedu._05mvcboot04.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandle {
    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String data = ex.getFieldError().getDefaultMessage();
        log.debug("data:"+data);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
    @ExceptionHandler
    public JsonResult doHandleConstraintViolationException(ConstraintViolationException ex){
        String data = ex.getMessage();
        log.debug("data="+data);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }
    //@ExceptionHandler
    public JsonResult doHandleThrowable(Throwable ex){
        String message = ex.getMessage();
        log.debug("message:"+message);
        return new JsonResult(StatusCode.OPERATION_FAILED,message);
    }
}
